package com.ckf.springbootswagger.controller;

import com.ckf.springbootswagger.service.UserService;
import io.swagger.annotations.Api;
import com.ckf.springbootswagger.entity.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 安详的苦丁茶
 * @version 1.0
 * @date 2020/3/13 9:52
 */

@Api(tags = "用户管理")
@RestController
public class UserController {

    /**
     * 日志
     */
    private Logger log = LoggerFactory.getLogger(UserController.class);

    Map<String, Object> outMap = new HashMap<String, Object>();

    @Autowired
    private UserService userService;


    /**
     * 全查询
     *
     * @return
     */
    @ApiOperation(value = "用户查询接口方式一")
    @GetMapping("/list")
    public List<User> select() {
        List<User> users = userService.list();

        if (users != null) {
            log.info("查询成功");
        } else {
            log.info("查询失败");
        }
        return users;
    }

    /**
     * 查询
     *
     * @return
     */
    @ApiOperation(value = "用户查询接口方式二")
    @GetMapping("/select")
    public Map<String, Object> select_user() {

        List<User> list = userService.list();

        if (list != null) {
            log.info("查询成功");
            outMap.put("user", list);
            return outMap;

        } else {
            log.info("查询失败");
        }

        return outMap;

    }


    /**
     * 添加用户
     *
     * @return
     */
    @ApiOperation("根据id添加用户的接口")
    @PostMapping("/insert")
    public Map<String, Object> insert(User user) {

        boolean flag = userService.save(user);

        if (flag) {
            log.info("添加成功");
            outMap.put("msg", "添加成功");
            log.info("添加成功");
        } else {
            outMap.put("msg", "添加失败");
            log.info("添加成功");
        }
        return outMap;
    }

    /**
     * 删除方式一
     *
     * @param id
     * @return
     */
    @ApiOperation("根据ID删除用户的接口")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "int")
    @DeleteMapping("/deleteUser")
    public Map<String, Object> deleteUser(Integer id) {

        if (id == null) {
            log.info("参数不能为空", id);
            outMap.put("msg", "参数不能为空");
            return outMap;
        }

        boolean flag = userService.removeById(id);

        if (flag) {
            log.info("删除成功={}", flag);
            outMap.put("msg", "删除成功");
            return outMap;

        }
        log.error("删除失败={}", flag);
        outMap.put("msg", "删除失败");

        return outMap;
    }


    /**
     * 删除方式二
     *
     * @param id
     * @return
     */
    @ApiOperation("根据ID删除用户的接口二")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "int")
    @DeleteMapping("/delete")
    public Map<String, Object> delete_User(Integer id) {

        if (id == null) {
            log.info("参数不能为空", id);
            outMap.put("msg", "参数不能为空");
            return outMap;
        }

        boolean flag = userService.removeById(id);

        if (flag) {
            log.info("删除成功", flag);
            outMap.put("msg", "删除成功");
            System.out.println(id);
            return outMap;

        } else {
            log.error("删除失败", flag);
            outMap.put("msg", "删除失败");
            outMap.put("msg", "参数不能为空");
        }

        return outMap;
    }

    /**
     * 修改
     *
     * @param user
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "根据id修改用户接口")
    public Map<String, Object> updateUserById(User user) {

        boolean flag = userService.updateById(user);

        if (flag) {
            outMap.put("code", "200");
            outMap.put("msg", "修改成功");
            log.info("修改成功");
        } else {
            outMap.put("code", "100");
            outMap.put("msg", "修改失败");
            log.info("修改失败");
        }
        return outMap;

    }
}
